<?php

$http = new swoole_websocket_server("0.0.0.0", 8811);

$http->set([
    
    'enable_static_handler'=>true,
    'document_root'=>"/www/wwwroot/ymh/swoole/public/static",
    'worker_num'=>5,
    
]);

$http->on("WorkerStart",function (swoole_server $server,$worker_id) {
  
    define('APP_PATH', __DIR__ . '/../application/');
    
//     // 加载基础文件
//     require __DIR__ . '/../thinkphp/base.php';
});


    $http->on('request',function ($request,$response) use($http){
        // 定义应用目录
        
        
        // 加载基础文件
        require_once __DIR__ . '/../thinkphp/base.php';
        
        if(isset($request->server)){
            foreach($request->server as $key=>$val){
                $_SERVER[strtoupper($key)]=$val;   
            } 
        }
        
        if(isset($request->header)){
            foreach($request->header as $key=>$val){
                $_SERVER[strtoupper($key)]=$val;
            }
        }
      
        $_GET=[];
        
        if(isset($request->get)){
            foreach($request->get as $key=>$val){
                $_GET[$key]=$val;
            }
        }
       
        
        if(isset($request->post)){
            foreach($request->post as $key=>$val){
                $_POST[$key]=$val;
            }
        }
        ob_start();
        
//            try {
               think\Container::get('app', [APP_PATH])
               ->run()
               ->send();
//             } catch (\Exception $e) {
//                dump($e);
//             } 
                
//             echo "<h1>Function</h1>".PHP_EOL;
//           var_dump(request()->action());
            
            $res=ob_get_contents();
            ob_end_clean();


            $response->end($res);
    
            //废除不需要了
//             $http->close($request->fd);
});

    $http->on('message', function (swoole_websocket_server $server, $frame) {
        
        
    });


$http->start();
